<template>

    <Card>
        <Input v-model="ruleName"/>
        <rule-tree v-if="dataLoad" @saved="ruleSaved"
                   :rule-data="ruleData"
                   :ruleAtoms="ruleAtoms" :rule-id="ruleId" :ruleName="ruleName" :ruleGroup="ruleGroup" :businessScenario="businessScenario"/>
    </Card>
</template>
<script>
    import ruiRules from '_c/rui-auto/rui-rules/index'
    import {loadRuleDataById} from '_p/basic/api/rule/rule'
    export default {
        name:'demo-rule-tree',
        components: {
            ruleTree:ruiRules
        },
        props: {

        },

        data() {
            return {
                ruleAtoms:[
                    {
                        key:'carColor',
                        name:"车颜色",
                        valueExpress:"car.carColor",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'custDic',
                        componentConfig:{
                            placeholder:'选择车颜色',
                            dicUrl:'/admin/dictdata/getByType/logType',
                            params:'',//json 表达式
                            method:'get'
                        //    返回 title：value：json对象数组
                        //
                        }
                    },
                    {
                        key:'components',
                        name:"自定义组件",
                        valueExpress:"car.carColor",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'component',
                        componentConfig:{
                            params:"{\"aa\":\"aa\",\"bb\":\"bb\"}",
                            isFull:'1',
                            placeholder:'你选一个试试',
                            componentPath:'demo/rules/component/demo-component'
                        }
                    },
                    {
                        key:'loanRate',
                        name:"利率",
                        valueExpress:"car.loanRate",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'number',
                        componentConfig:{
                            placeholder:'利率',
                            scale:2,
                            type:'percentage',
                            min:10,
                            max:99
                        }
                    },
                    {
                        key:'area',
                        name:"购车地",
                        valueExpress:"car.area",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'cascader',
                        componentConfig:{
                            placeholder:'选择购车地',
                            level:3,//层级
                            levelConfig:[
                                {
                                    url:'/admin/dictdata/getByType/logType',
                                    params:'',//json 表达式
                                    method:'get'
                                },
                                {
                                    url:'/admin/dictdata/getByType/logType',
                                    params:'',//json 表达式
                                    method:'get',
                                    superKey:'level1'//上一层作为哪个参数传递key
                                },
                                {
                                    url:'/admin/dictdata/getByType/logType',
                                    params:'',//json 表达式
                                    method:'get',
                                    superKey:'level2'//上一层作为哪个参数传递key
                                }
                            ]//层级获取数据配置
                            //    返回 title：value：json对象数组
                        }
                    },
                    {
                        key:'carType',
                        name:"车类型",
                        valueExpress:"car.carType",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'dic',
                        componentConfig:{
                            placeholder:'选择车颜色',
                            dicKey:'logType'
                        }
                    },
                    {
                        key:'carAmt',
                        name:"车款",
                        valueExpress:"car.amt",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'number',
                        componentConfig:{
                            placeholder:'车款金额',
                            scale:2,
                            type:'currency',
                            min:1000,
                            max:9999
                        }
                    },
                    {
                        key:'carCount',
                        name:"数量",
                        valueExpress:"car.count",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'number',
                        componentConfig:{
                            placeholder:'车数量',
                            scale:0,
                            type:'number',
                            min:1,
                            max:10
                        }
                    },
                    {
                        key:'carName',
                        name:"车名",
                        valueExpress:"car.name",
                        operatorScope:"cust",
                        operatorTypes:['in'],
                        component:'text',
                        componentConfig:{
                            placeholder:'车名',
                            minLength:10,
                            maxLength:20,
                            reg:'[0-9]+',
                            regError:'只能输入数字'
                        }
                    },
                    {
                        key:'carName1',
                        name:"车名1",
                        valueExpress:"car.name1",
                        operatorScope:"cust",
                        operatorTypes:['in'],
                        component:'text',
                        componentConfig:{
                            placeholder:'车名',
                            minLength:10,
                            maxLength:20,
                            reg:'[0-9]+',
                            regError:'只能输入数字'
                        }
                    },
                    {
                        key:'carName2',
                        name:"车名2",
                        valueExpress:"car.name2",
                        operatorScope:"cust",
                        operatorTypes:['in'],
                        component:'text',
                        componentConfig:{
                            placeholder:'车名',
                            minLength:10,
                            maxLength:20,
                            reg:'[0-9]+',
                            regError:'只能输入数字'
                        }
                    },
                    {
                        key:'buyDate',
                        name:"购车日期",
                        valueExpress:"car.buyDate",
                        operatorScope:"all",
                        operatorTypes:[],
                        component:'date',
                        componentConfig:{
                            placeholder:'购车日期',
                            minDate:'2018-09-10',
                            maxDate:'2021-12-31',
                            format:'yyyy-MM-dd',
                        }
                    }
                ],
                ruleName:'demo规则配置',
                ruleGroup:'ruleGroup',
                businessScenario:'businessScenario',
                ruleData:[],
                ruleId:'4386119490002796588',
                dataLoad:false
            }
        },

        created() {
            loadRuleDataById((this.ruleId&&this.ruleId!='')?this.ruleId:undefined).then(res=>{
                if(res.code='0000'){
                    if(res.data.length>0) {
                        this.ruleData.push(...res.data);
                    }
                    this.dataLoad = true;
                }
            });
        },

        watch: {
        },

        methods: {
            ruleSaved(ruleId){
                this.ruleId = ruleId;
                console.log(ruleId)
            }
        }
    }
</script>
